package cn.yunyichina.provider.pay.service;

import cn.yunyichina.provider.iface.entity.dto.pacity.PacityAccessTokenRequestDTO;
import cn.yunyichina.provider.pay.entity.PaymentSetting;
import cn.yunyichina.provider.pay.exception.PaySettingNotFoundException;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Lullaby on 2017/2/28.
 */
public class BaseAccessTokenService extends BaseService {

    public boolean checkGetWechatOrdinaryAccessTokenParameters(Map<String, String> params) {
        if (StringUtils.isEmpty(params.get("app_id"))) {
            return false;
        }

        return true;
    }

    public Map<String, String> buildGetWechatOrdinaryAccessTokenParameters(Map<String, String> params) throws PaySettingNotFoundException {
        String appId = params.get("app_id");

        PaymentSetting paymentSetting = this.getPaymentSetting(appId);
        String appSecret = paymentSetting.getSecretKey();

        Map<String, String> result = new HashMap<>();
        result.put("appid", appId);
        result.put("secret", appSecret);
        result.put("grant_type", "client_credential");

        return result;
    }

    public boolean checkGetPacityOrdinaryAccessTokenParameters(Map<String, String> params) {
        if (StringUtils.isEmpty(params.get("grant_type"))) return false;
        if (StringUtils.isEmpty(params.get("scope"))) return false;

        return true;
    }

    public PacityAccessTokenRequestDTO buildGetPacityOrdinaryAccessTokenRequestDTO(Map<String, String> params) {
        PacityAccessTokenRequestDTO requestDTO = new PacityAccessTokenRequestDTO();
//        requestDTO.setAppId(params.get("app_id"));
//        requestDTO.setMethod(Pacity.METHOD_ACCESS_TOKEN);
//        requestDTO.setCharset(Pacity.CHARSET);
//        requestDTO.setTimestamp(DateUtils.getDateTimeStr());
//        requestDTO.setSignType(Pacity.SIGNTYPE_RSA1);
//        requestDTO.setVersion(Pacity.VERSION_1_0);

        requestDTO.setGrantType(params.get("grant_type"));
        requestDTO.setScope(params.get("scope"));
        requestDTO.setCode(params.get("auth_code"));
        requestDTO.setRedirectUri(params.get("redirect_uri"));

//        requestDTO.setSign(null);

        return requestDTO;
    }

}
